package cn.edu.cug.cs.gtl.oi.ui.actions;

import javax.swing.*;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.DefaultMutableTreeNode;

import cn.edu.cug.cs.gtl.common.Pair;
import cn.edu.cug.cs.gtl.oi.render.Render;
import cn.edu.cug.cs.gtl.oi.ui.MainApp;
import cn.edu.cug.cs.gtl.oi.ui.projecttree.ProjectTreeNodeData;
import cn.edu.cug.cs.gtl.protos.Layer;
import cn.edu.cug.cs.gtl.protos.Map;
import cn.edu.cug.cs.gtl.protos.Project;
import com.openinventor.inventor.nodes.SoDrawStyle;
import com.openinventor.inventor.nodes.SoGroup;
import com.openinventor.inventor.nodes.SoNode;
import com.openinventor.inventor.nodes.SoSeparator;

public class ProjectTreeModelListener implements TreeModelListener {
    MainApp theApp=null;

    public ProjectTreeModelListener(MainApp theApp) {
        this.theApp = theApp;
    }

    @Override
    public void treeNodesChanged(TreeModelEvent e) {
        Object [] children = e.getChildren();
        if(children!=null && children.length>0){
            DefaultMutableTreeNode node =(DefaultMutableTreeNode) (children[0]);
            ProjectTreeNodeData projectTreeNodeData = (ProjectTreeNodeData)node.getUserObject();
            Object refObject = projectTreeNodeData.getRefObject();
            boolean visible = projectTreeNodeData.isChecked();
            SoNode sceneGraph = theApp.getMainWindow().getRenderPanel().getSceneGraph();
            if(refObject instanceof Project) {
                theApp.getMainWindow().getRenderPanel().getRender().setVisible((Project) refObject,visible);
            }
            else if(refObject instanceof Map) {
                theApp.getMainWindow().getRenderPanel().getRender().setVisible((Map) refObject,visible);
            }
            else if(refObject instanceof Layer) {
                theApp.getMainWindow().getRenderPanel().getRender().setVisible((Layer) refObject,visible);
            }
            else{

            }
            this.theApp.getMainWindow().getRenderPanel().getViewer().viewAll();
            this.theApp.getMainWindow().getRenderPanel().getViewer().getRenderArea().invalidate();
        }
    }

    @Override
    public void treeNodesInserted(TreeModelEvent e) {

    }

    @Override
    public void treeNodesRemoved(TreeModelEvent e) {

    }

    @Override
    public void treeStructureChanged(TreeModelEvent e) {

    }
}
